package com.chinacoal.microservice.impl.controller;


import com.chinacoal.microservice.impl.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinacoal.microservice.api.ForeignSystemTaskClient;
import com.chinacoal.microservice.api.SysCenterWeb;
import com.chinacoal.microservice.api.model.ApproveTaskParam;
import com.chinacoal.microservice.api.model.ForeignTaskParam;
import com.chinacoal.microservice.api.model.entity.UtaskInfoEntity;
import com.chinacoal.microservice.api.model.resource.BusinessData;
import com.chinacoal.microservice.api.model.resource.UnionTaskVar;
import com.chinacoal.microservice.model.syscenter.SysCenter;
import com.chinacoal.microservice.util.result.CodeMsg;
import com.chinacoal.microservice.util.result.Result;

/**
 * @author linp
 * @version V1.0
 * @Description:待办审批处理控制类
 * @date 2020年4月21日 下午3:55:07
 */
@Controller
public class ForeignSystemTaskController implements ForeignSystemTaskClient {

    @Autowired
    private ForeignSystemTaskService foreignSystemTaskService;
    @Autowired
    private PurchaseTaskService purchaseTaskService;
    @Autowired
    private CcmsTaskService ccmsTaskService;
    @Autowired
    private OaTaskService oaTaskService;
    @Autowired
    private ErpTaskService erpTaskService;
    @Autowired
    private SysCenterWeb sysCenterWeb;
    @Autowired
    private UtaskInfoService utaskInfoService;
    @Autowired
    private CCIPTaskService ccipTaskService;

    @Override
    @ResponseBody
    public Result<BusinessData> getTaskDetail(ForeignTaskParam foreignTaskParam) {
        try {
            // 1、根据待办Id,查询待办详情信息
            UtaskInfoEntity utask = utaskInfoService.getUtaskInfoById(foreignTaskParam.getTaskId());
            if (utask == null) {
                throw new Exception("未找到待办信息，请确认传入的待办Id:" + foreignTaskParam.getTaskId() + "是否有误!");
            }
            // 2、根据系统注册码，查询应用注册详情信息
            Result<SysCenter> result = sysCenterWeb.getSysByCode(utask.getSysCode());
            if (result == null) {
                throw new Exception("调用获取应用注册信息服务失败!");
            }
            SysCenter sysCenter = result.getData();
            if (sysCenter == null) {
                throw new Exception("系统标识码" + utask.getSysCode() + "在应用中心未注册，请联系中台管理员!");
            }
            // 3、参数赋值
            foreignTaskParam.setSysCode(sysCenter.getSysCode());
            foreignTaskParam.setBusinessId(utask.getBusinessId());
            foreignTaskParam.setBizCode(utask.getBizCode());
            foreignTaskParam.setSysTaskId(utask.getSysTaskId());

            // 4、调用服务
            if (UnionTaskVar.SYS_CODE_PURCHASE.equals(foreignTaskParam.getSysCode())) { // 采购电商系统
                return purchaseTaskService.getDetails(foreignTaskParam);

            } else if (UnionTaskVar.SYS_CODE_CCIP.equals(foreignTaskParam.getSysCode())) { // 协同指挥平台
                return ccmsTaskService.getDetails(foreignTaskParam);

            } else if (UnionTaskVar.SYS_CODE_OA.equals(foreignTaskParam.getSysCode())) { // OA系统平台
                return oaTaskService.getTaskDetails(foreignTaskParam);

            } else if (UnionTaskVar.SYS_CODE_ERP.equals(foreignTaskParam.getSysCode())) { // ERP系统平台
                return erpTaskService.getErpDetail(foreignTaskParam);

            } else if (UnionTaskVar.SYS_CODE_CCIP2.equals(foreignTaskParam.getSysCode())) { // 门户协同智慧平台
                return ccipTaskService.getDetails(foreignTaskParam);
            } else {

            }

            return Result.success(new BusinessData());
			
/*			
			// 3、调用第三方系统服务获取待办详情
			HttpHeaders headers = new HttpHeaders();
			// 以表单的方式提交
			headers.setContentType(MediaType.APPLICATION_JSON_UTF8);	
			
			// 将请求头部和参数合成一个请求
			Map<String,Object> map = new HashMap<String,Object>() ;
			map.put("reqTerminal", utaskDetailParam.getReqTerminal()) ;	// 请求终端
			map.put("extParams", utaskDetailParam.getExtParams()) ;		// 其他扩展参数
			if("SYS-20200509-1000".equals(utask.getSysCode())) {	
				// 协同平台内部待办
				map.put("businessId", utask.getBusinessId()) ;
				map.put("bizCode", utask.getBizCode()) ;
			}else {	 
				//非协同平台待办
				map.put("sysTaskId", utask.getSysTaskId()) ;
			}
			HttpEntity<Map<String,Object>> requestEntity = new HttpEntity<>(map, headers);
			
			// 请求地址和方式
			String url = sysCenter.getTaskDetailUrl() ;
			if(url == null || "".equals(url)) {
				throw new Exception(sysCenter.getSysName()+"的注册信息中未提供待办详情获取地址，请联系中台管理员!") ;
			}
			// 请求方式
			sysCenter.getTaskDetailMethod() ;
			
			HttpMethod method = HttpMethod.POST;
			// 执行HTTP请求，将返回的结构使用ResultVO类格式化
			ResponseEntity<BusinessData> response = restTemplate.exchange(url, method, requestEntity, BusinessData.class);
			
			BusinessData businessData = response.getBody();
			
			return Result.success(businessData);
*/

        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
        }
    }

    @Override
    @ResponseBody
    public Result<Object> approveTask(ApproveTaskParam approveTaskParam) {
        try {
            return foreignSystemTaskService.approveAndUpdateBusiness(approveTaskParam);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
        }
    }

    @Override
    @ResponseBody
    public Result<Object> downloadAttach(ForeignTaskParam foreignTaskParam) {
        return null;
    }

    @Override
    public ResponseEntity<byte[]> downloadAttach(String sysCode, String fileName, String filePath, String sysTaskId) {
        ResponseEntity<byte[]> result = null;
        try {
            if (UnionTaskVar.SYS_CODE_PURCHASE.equals(sysCode)) { // 采购电商系统

            } else if (UnionTaskVar.SYS_CODE_CCIP.equals(sysCode)) { // 协同指挥平台

            } else if (UnionTaskVar.SYS_CODE_OA.equals(sysCode)) { // OA系统平台
                return oaTaskService.downloadAttach(fileName, filePath, sysTaskId);
            } else if (UnionTaskVar.SYS_CODE_ERP.equals(sysCode)) { // ERP系统平台
                return erpTaskService.downloadAttach(fileName, filePath);
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    @ResponseBody
    public Result<Object> approveCard(String sysCode, String businessId, String bizCode, String taskId, String instanceId, String approvalOpinion, String opcode, String userCode) {
        try {
            if ("-1".equals(businessId)) {
                return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs("审批失败"));
            } else {
                return Result.success("审批成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(CodeMsg.EXECUTE_ERROR.fillArgs(e.getMessage()));
        }
    }

}
